package com.cs.common.utils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import com.cs.common.exception.CSException;
import com.cs.common.utils.exception.ReflectionException;

public class ReflectionUtil {

	public static Object invokeMethodOnClass(String className, String methodName, Class[] paramTypes, Object[]params, String fileName) throws ReflectionException{
		Class parserClass = null; 
		Method method = null;
		Object result = null;
		
		try {
			
			Class[] paramConstructorTypes = {String.class};
			Object[] paramConstructorValues = {fileName};
			parserClass = Class.forName(className).getConstructor(paramConstructorTypes).getDeclaringClass();
			method = parserClass.getMethod(methodName, paramTypes);			
			result = method.invoke(parserClass.getConstructor(paramConstructorTypes).newInstance(paramConstructorValues), params);		
			
		} catch (ClassNotFoundException exception) {
			ReflectionException reflectionException = null;
			exception.printStackTrace(System.out);
			reflectionException = new ReflectionException(exception , CSException.LAYER_ACTION,CSException.ACTION_LISTS);
			throw reflectionException;			
		} catch (SecurityException exception) {			
			ReflectionException reflectionException = null;
			exception.printStackTrace(System.out);
			reflectionException = new ReflectionException(exception , CSException.LAYER_ACTION,CSException.ACTION_LISTS);
			throw reflectionException;		
		} catch (NoSuchMethodException exception) {			
			ReflectionException reflectionException = null;
			exception.printStackTrace(System.out);
			reflectionException = new ReflectionException(exception , CSException.LAYER_ACTION,CSException.ACTION_LISTS);
			throw reflectionException;		
		} catch (IllegalArgumentException exception) {			
			ReflectionException reflectionException = null;
			exception.printStackTrace(System.out);
			reflectionException = new ReflectionException(exception , CSException.LAYER_ACTION,CSException.ACTION_LISTS);
			throw reflectionException;		
		} catch (IllegalAccessException exception) {
			ReflectionException reflectionException = null;
			exception.printStackTrace(System.out);
			reflectionException = new ReflectionException(exception , CSException.LAYER_ACTION,CSException.ACTION_LISTS);
			throw reflectionException;		
		} catch (InvocationTargetException exception) {
			ReflectionException reflectionException = null;
			exception.printStackTrace(System.out);
			reflectionException = new ReflectionException(exception , CSException.LAYER_ACTION,CSException.ACTION_LISTS);
			throw reflectionException;		
		} catch (InstantiationException exception) {
			ReflectionException reflectionException = null;
			exception.printStackTrace(System.out);
			reflectionException = new ReflectionException(exception , CSException.LAYER_ACTION,CSException.ACTION_LISTS);
			throw reflectionException;		
		}
		return result;
	}

}
